package vn.edu.nuce.bean;

import java.io.IOException;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;

import vn.edu.nuce.model.User;
import vn.edu.nuce.service.LoginService;

@ManagedBean(name="loginBean")
public class LoginBean {
	@ManagedProperty(value="#{LoginService}")
	LoginService loginService;
	private String tenDangNhap;
	private String matKhau;
	private User user;
	private boolean quanLy;//trang thai menu
	private boolean giaoVien;
    public static final String AUTH_ID = "au_key_id";
    public static final String AUTH_QL = "au_key_ql";
    public static final String AUTH_GV = "au_key_gv";
	
	public String login(){
		boolean userNull = true;
		try {
			user = getLoginService().getUserName(tenDangNhap, matKhau);
			userNull = false;
		} catch (Exception e) {
		}
		
		if (tenDangNhap.contains("'") || tenDangNhap.contains("\"") || tenDangNhap.contains("--") || matKhau.contains("'") || matKhau.contains("\"") || matKhau.contains("--") || userNull) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Thông tin tài khoản không chính xác!", ""));
            return "";
        } else {
        	FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(AUTH_QL, user.isQuanLy());
    		FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(AUTH_GV, !user.isQuanLy());
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(AUTH_ID, user.getId());
            if (user.isQuanLy()){
            	return "quan-ly";
            } else {
            	return "dang-ky";
            }
            
        }
	}
	
	public void logout(){
		FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove(AUTH_QL);
		FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove(AUTH_GV);
		FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove(AUTH_ID);
		try {
			FacesContext.getCurrentInstance().getExternalContext().redirect("index");
		} catch (IOException e) {
			e.printStackTrace();
		}
		//return "index.xhtml";
	}
	public LoginService getLoginService() {
		return loginService;
	}
	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}
	public String getTenDangNhap() {
		return tenDangNhap;
	}
	public void setTenDangNhap(String tenDangNhap) {
		this.tenDangNhap = tenDangNhap;
	}
	public String getMatKhau() {
		return matKhau;
	}
	public void setMatKhau(String matKhau) {
		this.matKhau = matKhau;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public boolean isQuanLy() {
		return (Boolean) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(AUTH_QL);
	}

	public void setQuanLy(boolean quanLy) {
		this.quanLy = quanLy;
	}

	public boolean isGiaoVien() {
		return (Boolean) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(AUTH_GV);
	}

	public void setGiaoVien(boolean giaoVien) {
		this.giaoVien = giaoVien;
	}
	
	

}
